// (c) Copyright 2001-2009 Hewlett-Packard Development Company, L.P.
// Use is subject to license terms.

package demo.basic.hp.registry;

import org.systinet.uddi.InvalidParameterException;
import org.systinet.uddi.client.v3.UDDIException;
import org.systinet.uddi.client.v3.UDDIInquiryStub;
import org.systinet.uddi.client.v3.UDDIPublishStub;
import org.systinet.uddi.client.v3.UDDISecurityStub;
import org.systinet.uddi.client.v3.UDDI_Inquiry_PortType;
import org.systinet.uddi.client.v3.UDDI_Publication_PortType;
import org.systinet.uddi.client.v3.UDDI_Security_PortType;
import org.systinet.uddi.client.v3.struct.AccessPoint;
import org.systinet.uddi.client.v3.struct.AuthToken;
import org.systinet.uddi.client.v3.struct.BindingTemplate;
import org.systinet.uddi.client.v3.struct.BusinessService;
import org.systinet.uddi.client.v3.struct.BusinessServiceArrayList;
import org.systinet.uddi.client.v3.struct.Find_business;
import org.systinet.uddi.client.v3.struct.Find_service;
import org.systinet.uddi.client.v3.struct.Get_authToken;
import org.systinet.uddi.client.v3.struct.BusinessDetail;
import org.systinet.uddi.client.v3.struct.BusinessEntityArrayList;
import org.systinet.uddi.client.v3.struct.BusinessEntity;
import org.systinet.uddi.client.v3.struct.ListDescription;
import org.systinet.uddi.client.v3.struct.Save_business;
import org.systinet.uddi.client.v3.struct.Description;
import org.systinet.uddi.client.v3.struct.Name;
import org.systinet.uddi.client.v3.struct.Save_tModel;
import org.systinet.uddi.client.v3.struct.ServiceDetail;
import org.systinet.uddi.client.v3.struct.ServiceInfo;
import org.systinet.uddi.client.v3.struct.ServiceInfoArrayList;
import org.systinet.uddi.client.v3.struct.ServiceList;
import org.systinet.uddi.client.v3.struct.TModel;
import org.systinet.uddi.client.v3.struct.TModelDetail;
import org.systinet.uddi.client.v3.struct.TModelInstanceInfo;
import org.systinet.uddi.client.wsdl2uddi.v3.Wsdl2uddiApi;
import org.systinet.uddi.client.wsdl2uddi.v3.Wsdl2uddiStub;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.Get_wsdlServiceInfo;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.WsdlServiceInfo;
import org.systinet.uddi.client.wsdl2uddi.v3.struct.WsdlServiceInfos;

import javax.xml.soap.SOAPException;
import java.util.Iterator;

public class UDDIFinding {

    public static final String URL_INQUIRY = "http://localhost:8089/uddi/inquiry";
    public static final String URL_SECURITY = "http://localhost:8089/uddi/security";
    public static final String URL_WSDL_INQUIRY = "http://localhost:8089/uddi/wsdl2uddi";
    
    private static final String USER_NAME = "demo_john";
    private static final String USER_PASSWORD = "demo_john";
    private UDDI_Inquiry_PortType inquiry = null;
    private Wsdl2uddiApi wsdlInquiry = null;
    private UDDI_Security_PortType security = null;
    private AuthToken authToken = null;

    public UDDIFinding() {
    	try {
    		inquiry = UDDIInquiryStub.getInstance(URL_INQUIRY);
			security = UDDISecurityStub.getInstance(URL_SECURITY);
			wsdlInquiry = Wsdl2uddiStub.getInstance(URL_WSDL_INQUIRY);
			authToken = security.get_authToken(new Get_authToken(USER_NAME, USER_PASSWORD));
		} catch (SOAPException e) {
			e.printStackTrace();
		} catch (UDDIException e) {
			e.printStackTrace();
		} catch(InvalidParameterException e) {
			e.printStackTrace();
		}
    }

    public void inquireRegistry(String serviceName) {
        System.out.println("Finding service name = " + serviceName + " ...");
        int MAX_ROWS = 10;
        
        Find_service find_service = new Find_service();
        try {
			find_service.addName(new Name(serviceName));
			find_service.addFindQualifier("approximateMatch");
			find_service.setMaxRows(new Integer(MAX_ROWS));
			ServiceList serviceList = inquiry.find_service(find_service);
			printServiceList(serviceList);
			
		} catch (InvalidParameterException e) {
			e.printStackTrace();
		} catch (UDDIException e) {
			e.printStackTrace();
		}
        System.out.println(" done");
    }
    
    public void findWSDLLocation(String serviceKey) {
    	Get_wsdlServiceInfo get_wsdlService = new Get_wsdlServiceInfo();
    	get_wsdlService.setAuthInfo(authToken.getAuthInfo());
    	try {
			get_wsdlService.setServiceKey(serviceKey);
			WsdlServiceInfos wsdlInfos = wsdlInquiry.get_wsdlServiceInfo(get_wsdlService);
			int position = 1;
			for(Iterator<WsdlServiceInfo> iter = wsdlInfos.getWsdlServiceInfoArrayList().iterator();iter.hasNext();) {
				WsdlServiceInfo wsdlInfo = iter.next();
				System.out.println("WSDL Location "+position+" :"+wsdlInfo.getWsdlLocation());
				position++;
			}
		} catch (InvalidParameterException e) {
			e.printStackTrace();
		} catch (UDDIException e) {
			e.printStackTrace();
		}
    }
    
    public void printServiceList(ServiceList serviceList) {
        System.out.println();
        ListDescription listDescription = serviceList.getListDescription();
        if (listDescription!=null) {
            // list description is mandatory part of result, if the resultant list is subset of available data
            int includeCount = listDescription.getIncludeCount();
            int actualCount = listDescription.getActualCount();
            int listHead = listDescription.getListHead();
            System.out.println("Displaying "+includeCount+" of "+actualCount+", starting at position " + listHead);
        }

        ServiceInfoArrayList serviceInfoArrayList = serviceList.getServiceInfoArrayList();
        if (serviceInfoArrayList==null) {
            System.out.println("Nothing found");
            return;
        }

        int position = 1;
        for (Iterator iterator = serviceInfoArrayList.iterator(); iterator.hasNext();) {
            ServiceInfo serviceInfo = (ServiceInfo) iterator.next();
            System.out.println("Service "+position+" : "+serviceInfo.getServiceKey());
            System.out.println(serviceInfo.toXML());
            System.out.println();
            String serviceKey = serviceInfo.getServiceKey();
            findWSDLLocation(serviceKey);
            System.out.println("********************************************************");
            position++;
        }
    }

    public static void main(String[] args) throws Exception {
        UDDIFinding demo = new UDDIFinding();
        demo.inquireRegistry(args[0]);
    }
}
